package com.vpa.main;

import java.io.File;
import java.io.PrintStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;

public class DataLogger 
{
	File fl;
	PrintStream os;
	DateFormat dateFormat;
	Date date;
	public DataLogger() throws IOException
	{
		dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		date = new Date();
		File fll = new File(Environment.getExternalStorageDirectory().getPath() + "/VPA/");
		fll.mkdirs();
		fl = new File(Environment.getExternalStorageDirectory().getPath() + "/VPA/" + dateFormat.format(date) + ".vpa");
		fl.createNewFile();
		if(!fl.canWrite())
			throw new IOException("Can't write to the file (check permissions)");
		os = new PrintStream(fl);
	}
	
	public void appendLine(String s, boolean includeTimestamp)
	{
		if(os == null)
			return;
		if(includeTimestamp)
			os.print(dateFormat.format(date) + ": ");
		os.println(s);
	}
	
	public void finish()
	{
		if(os == null)
			return;
		os.flush();
	}
}
